#include "databasewidget.h"

#include "qruntimedatabaseview.h"

#include "../../include/abstractcore.h"
#include "../../lib/corelib/styledbar.h"
#include "../../lib/corelib/stylehelper.h"
#include "../../lib/shared/qproject.h"

#include <QVBoxLayout>
#include <QHBoxLayout>

#include <QPainter>
#include <QFontMetrics>
#include <QLinearGradient>
#include <QMouseEvent>
#include <QMessageBox>

static void drawSeparator(QPainter *p,QPoint top,QPoint bottom)
{
    QLinearGradient grad(top,bottom);

    grad.setColorAt(0,QColor(255,255,255,20));
    grad.setColorAt(0.4,QColor(255,255,255,60));
    grad.setColorAt(0.7,QColor(255,255,255,50));
    grad.setColorAt(1,QColor(255,255,255,40));
    p->setPen(QPen(grad,0));
    p->drawLine(top,bottom);
    grad.setColorAt(0,QColor(0,0,0,30));
    grad.setColorAt(0.4,QColor(0,0,0,70));
    grad.setColorAt(0.7,QColor(0,0,0,70));
    grad.setColorAt(1,QColor(0,0,0,40));
    p->setPen(QPen(grad,0));
    p->drawLine(top-QPoint(1,0),bottom-QPoint(1,0));
}

TabWidget::TabWidget(QWidget *parent):
    QWidget(parent)
{

}

void TabWidget::addItems(const QString &item)
{
    tagTab tab;
    tab.m_text=item;
    QFontMetrics fm(font());
    tab.m_width=fm.width(item)+24;
    m_items.append(tab);
    this->update();
}

void TabWidget::select(QString str)
{
    m_select=str;
    this->update();
}

void TabWidget::select(int index)
{
    if(index<0 || index>=m_items.size())
    {
        return;
    }
    m_select=m_items.at(index).m_text;
    this->update();
}

QSize TabWidget::sizeHint()const
{
    int wid=0;
    for(int i=0;i<m_items.size();i++)
    {
        wid+=m_items.at(i).m_width;
    }
    return QSize(wid,25);
}

void TabWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);

    tagTab tab;
    int pt=0;
    QRect re=rect();
    QLinearGradient grad(QPoint(0,0),QPoint(0,re.height()-1));
    grad.setColorAt(0,QColor(247,247,247));
    grad.setColorAt(1,QColor(205,205,205));

    QFontMetrics fm(font());
    int baseline=(re.height()+fm.ascent())/2-1;
    for(int i=0;i<m_items.size();i++)
    {
        tab=m_items.at(i);

        if(tab.m_text==m_select)
        {
            p.setPen(StyleHelper::borderColor());
            p.drawLine(pt-1,0,pt-1,re.height());
            p.fillRect(QRect(pt,0,tab.m_width,re.height()+1),grad);
            if(i!=0)
            {
                p.setPen(QColor(255,255,255,170));
                p.drawLine(pt,0,pt,re.height());
            }
            p.setPen(StyleHelper::borderColor());
            p.drawLine(pt,re.height()-1,pt+tab.m_width+1,re.height()-1);
            p.setPen(Qt::black);
            p.drawText(pt+12,baseline,tab.m_text);
            pt+=tab.m_width;
            p.setPen(StyleHelper::borderColor());
            p.drawLine(pt,0,pt,re.height()-2);
            p.setPen(QColor(0,0,0,20));
            p.drawLine(pt+1,0,pt+1,re.height()-2);
            p.setPen(QColor(255,255,255,170));
            p.drawLine(pt-1,0,pt-1,re.height()-2);

        }
        else
        {
            if(i==0)
            {
                drawSeparator(&p,QPoint(pt,0),QPoint(pt,re.height()-1));
            }
            p.setPen(StyleHelper::panelTextColor());
            p.drawText(pt+13,baseline,tab.m_text);
            pt+=tab.m_width;
            drawSeparator(&p,QPoint(pt,0),QPoint(pt,re.height()-1));

        }
    }
}

void TabWidget::mousePressEvent(QMouseEvent *e)
{
    int pt=e->pos().x();

    int wid=0;
    for(int i=0;i<m_items.size();i++)
    {
        if(pt>wid && pt<wid+m_items.at(i).m_width)
        {
            if(m_items.at(i).m_text!=m_select)
            {
                emit clicked(i);
                return;
            }
        }
        wid+=m_items.at(i).m_width;
    }
}


DatabaseWidget::DatabaseWidget(AbstractCore* core,QWidget* parent):
    QMainWindow(parent),
    m_core(core),
    m_styledbar(new StyledBar(this)),
    m_stackedWidget(new QStackedWidget(this))
{

    m_runtimeView=new QRuntimeDatabaseView(core,this);

    QWidget* wid=new QWidget(this);
    this->setCentralWidget(wid);
    QVBoxLayout *l=new QVBoxLayout();
    wid->setLayout(l);

    l->setMargin(0);
    l->setSpacing(0);

    l->addWidget(m_styledbar);
    l->addWidget(m_stackedWidget);


    QHBoxLayout *w=new QHBoxLayout;

    w->setMargin(0);
    w->setSpacing(0);
    m_tabWidget=new TabWidget;
    w->addWidget(m_tabWidget);
    w->addWidget(m_runtimeView->toolBar());
    w->setStretchFactor(m_tabWidget,1);
    m_styledbar->setLayout(w);

    addWidget("Runtime Database",m_runtimeView);
    select(0);

    connect(m_tabWidget,SIGNAL(clicked(int)),this,SLOT(select(int)));

    connect(QProject::project(),SIGNAL(openProject()),this,SLOT(projectOpen()));
    connect(QProject::project(),SIGNAL(beforeClosed()),this,SLOT(projectClosed()));
}

void DatabaseWidget::addWidget(QString name, QWidget *page)
{
    m_tabWidget->addItems(name);
    m_stackedWidget->removeWidget(page);
    m_stackedWidget->addWidget(page);
}

void DatabaseWidget::select(int index)
{
    m_tabWidget->select(index);
    m_stackedWidget->setCurrentIndex(index);
}

void DatabaseWidget::projectOpen()
{
    m_runtimeView->clear();

    int count=QProject::project()->getRuntimeDataCount();
    for(int i=0;i<count;i++)
    {
        m_runtimeView->addRuntime(QProject::project()->getRuntimeData(i));
    }
}

void DatabaseWidget::projectClosed()
{
    m_runtimeView->clear();
}
